package com.sky.gray.controller;

import com.sky.gray.common.Response;
import com.sky.gray.domain.GrayUser;
import com.sky.gray.service.IGrayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin("*")
@RestController
@RequestMapping("/api/gray")
public class GrayController {

    @Autowired
    private IGrayService grayService;

    @RequestMapping(value = "/query_users", method = RequestMethod.GET)
    public Response<List<GrayUser>> queryGrayUsers() {
        try {
            List<GrayUser> grayUsers = grayService.queryUserList();
            return Response.<List<GrayUser>>builder()
                    .code(Response.ResponseStatus.SUCCESS.getCode())
                    .message(Response.ResponseStatus.SUCCESS.getInfo())
                    .data(grayUsers)
                    .build();
        } catch (Exception e) {
            log.error("查询灰度用户列表错误：{}", e.getMessage(), e);
            return Response.<List<GrayUser>>builder()
                    .code(Response.ResponseStatus.ERROR.getCode())
                    .message(Response.ResponseStatus.ERROR.getInfo())
                    .build();
        }
    }

    @RequestMapping(value = "/query_user", method = RequestMethod.POST)
    public Response<GrayUser> queryGrayUser(@RequestBody GrayUser user) {
        try {
            GrayUser grayUser = grayService.queryUserById(user);
            return Response.<GrayUser>builder()
                    .code(Response.ResponseStatus.SUCCESS.getCode())
                    .message(Response.ResponseStatus.SUCCESS.getInfo())
                    .data(grayUser)
                    .build();
        } catch (Exception e) {
            log.error("查询灰度用户：{}出错：{}", user.getUserId(), e.getMessage(), e);
            return Response.<GrayUser>builder()
                    .code(Response.ResponseStatus.ERROR.getCode())
                    .message(Response.ResponseStatus.ERROR.getInfo())
                    .build();
        }
    }

    @RequestMapping(value = "/add_user", method = RequestMethod.POST)
    public Response<Boolean> addGrayUser(@RequestBody GrayUser user) {
        try {
            grayService.addGrayUser(user);
            return Response.<Boolean>builder()
                    .code(Response.ResponseStatus.SUCCESS.getCode())
                    .message(Response.ResponseStatus.SUCCESS.getInfo())
                    .data(Boolean.TRUE)
                    .build();
        } catch (Exception e) {
            log.error("新增灰度用户出错：{}", e.getMessage(), e);
            return Response.<Boolean>builder()
                    .code(Response.ResponseStatus.ERROR.getCode())
                    .message(Response.ResponseStatus.ERROR.getInfo())
                    .data(Boolean.FALSE)
                    .build();
        }
    }

    @RequestMapping(value = "/update_user", method = RequestMethod.POST)
    public Response<Boolean> updateGrayUser(@RequestBody GrayUser user) {
        try {
            grayService.updateGrayUser(user);
            return Response.<Boolean>builder()
                    .code(Response.ResponseStatus.SUCCESS.getCode())
                    .message(Response.ResponseStatus.SUCCESS.getInfo())
                    .data(Boolean.TRUE)
                    .build();
        } catch (Exception e) {
            log.error("更新灰度用户出错：{}", e.getMessage(), e);
            return Response.<Boolean>builder()
                    .code(Response.ResponseStatus.ERROR.getCode())
                    .message(Response.ResponseStatus.ERROR.getInfo())
                    .data(Boolean.FALSE)
                    .build();
        }
    }

    @RequestMapping(value = "/delete_user", method = RequestMethod.POST)
    public Response<Boolean> deleteGrayUser(@RequestBody GrayUser user) {
        try {
            grayService.deleteGrayUser(user);
            return Response.<Boolean>builder()
                    .code(Response.ResponseStatus.SUCCESS.getCode())
                    .message(Response.ResponseStatus.SUCCESS.getInfo())
                    .data(Boolean.TRUE)
                    .build();
        } catch (Exception e) {
            log.error("删除灰度用户出错：{}", e.getMessage(), e);
            return Response.<Boolean>builder()
                    .code(Response.ResponseStatus.ERROR.getCode())
                    .message(Response.ResponseStatus.ERROR.getInfo())
                    .data(Boolean.FALSE)
                    .build();
        }
    }
}
